package com.tanhua.server.controller;
import com.tanhua.domain.db.User;
import com.tanhua.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


/**
 * 用户控制层
 */
@RestController
@RequestMapping("/user")
public class LoginController {

    @Autowired
    private UserService userService;

    /**
     * 根据手机号查询用户
     * ResponseEntity 响应内容 spring封装的响应http请求的对象
     *                  不能修改，与前端约定好的
     */
    @RequestMapping(value = "/findUser",method = RequestMethod.GET)
    public ResponseEntity findUser(String mobile){
        System.out.println("进入了方法");
        //调用业务层 返回结果
        return userService.findByMobile(mobile);
    }

    /**
     * 新增用户
     */
    @PostMapping("/saveUser")
    public ResponseEntity saveUser(@RequestBody User user){
        System.out.println("进入方法");
        userService.saveUser(user);
        return ResponseEntity.ok(null);// 状态码为200
    }

    /**
     * 登陆注册，发送验证码
     * @param paramMap
     * @return
     */
    @PostMapping("/login")
    public ResponseEntity sendValidateCode(@RequestBody Map<String,String> paramMap){

        //获取手机号码
        String phone = paramMap.get("phone");
        //调用业务发送验证码
        userService.sendValidateCode(phone);
        // 响应结果
        return ResponseEntity.ok(null);
    }

    /**
     * 登陆注册，校验验证码
     * @param paramMap
     * @return
     */
    @PostMapping("/loginVerification")
    public ResponseEntity loginVerification(@RequestBody Map<String,String> paramMap){
        //手机号码
        String phone = paramMap.get("phone");
        //验证码
        String verificationCode = paramMap.get("verificationCode");
        //调用业务 校验，生成token令牌
        Map<String,Object> result = userService.loginVerification(phone,verificationCode);
        return ResponseEntity.ok(result);
    }
}